//
//  MVPViewController.m
//  DebugDemo
//
//  Created by XFB on 2018/11/15.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "MVPViewController.h"
#import "Present.h"
#import "MVPCell.h"
#import "DataSource.h"
#import "Model.h"

static NSString *const reuserId = @"MVPCell";

@interface MVPViewController () <PresentDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) Present *present;
@property (nonatomic, strong) DataSource *dataSource;

@end

@implementation MVPViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"MVP面向协议编程";
}

- (void)setupUI
{
    self.present = [[Present alloc] init];
    __weak typeof(self) weakSelf = self;

    self.dataSource = [[DataSource alloc] initWithIdentifier:reuserId configureBlock:^(MVPCell * cell, Model *model, NSIndexPath * _Nonnull indexPath) {
        cell.delegate = weakSelf.present;
        cell.indexPath = indexPath;
        cell.nameLabel.text = model.name;
        cell.numberLabel.text = model.count;
    } selectBlock:^(NSIndexPath * _Nonnull indexPath) {
        NSLog(@"选择了%ld行", indexPath.row);
    }];

    [self.dataSource addDataArray:self.present.dataArray];
    
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self.dataSource;
    self.tableView.delegate = self.dataSource;
    self.present.delegate = self;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:@"MVPCell" bundle:nil] forCellReuseIdentifier:reuserId];
    }
    return _tableView;
}

#pragma mark --- <PresentDelegate>

- (void)reloadDataForUI
{
    [self.dataSource addDataArray:self.present.dataArray];
    [self.tableView reloadData];
}

@end
